/*
 * Example of drawing double-buffered graphics to a window.
 */
package hybris;

import java.util.Random;

/**
 *
 * @author menderleit
 */
public class Hybris implements Runnable {

    private int screenWidth, screenHeight, screenDepth, refreshRate;
    private Random rnd;
    private int numStars;
    private long pSpeed;
    private int pSize;
    private int pSizeSpeed;
    private int pSizeGrow;
    private int pAlphaMax;

    public Hybris(int screenWidth, int screenHeight, int screenDepth, int refreshRate, int detail) {
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;
        this.screenDepth = screenDepth;
        this.refreshRate = refreshRate;
        rnd = new Random();
        switch (detail) {
            case 0:
                numStars = 25000;
                pAlphaMax = 95;
                pSpeed = 5;
                pSize = 10;
                pSizeSpeed = 10;
                pSizeGrow = 2;
                break;
            case 1:
                numStars = 15000;
                pAlphaMax = 127;
                pSpeed = 15;
                pSize = 10;
                pSizeSpeed = 25;
                pSizeGrow = 4;
                break;
            case 2:
                numStars = 10000;
                pAlphaMax = 159;
                pSpeed = 25;
                pSize = 10;
                pSizeSpeed = 40;
                pSizeGrow = 6;
                break;
        }
    }

    @Override
    public void run() {
        FullScreen fs = new FullScreen(screenWidth, screenHeight, screenDepth, refreshRate);

        Intro intro = new Intro(fs, numStars, pSpeed, pSize, pSizeSpeed, pSizeGrow, pAlphaMax);
        Part1 part1 = new Part1(fs);

        // Short pause before starting.
        long time = System.currentTimeMillis();
        while (System.currentTimeMillis() - time < 1500) {
        }

        // Start the music.
        MP3 music = new MP3("hybris2.mp3");
        music.play();

        // Create a couple of variables for timing control.
        // The difference in milliseconds between frames will be in difTime.
        long oldTime = System.nanoTime() / 1000000;
        long difTime;

        int demoPart = 0;

        intro.start();
        part1.start();
        
        // This is the main loop.
        while (fs.getRunFlag()) {
            // Update the frame-timing.
            long nt = System.nanoTime() / 1000000;
            difTime = nt - oldTime;
            oldTime = nt;

            switch (demoPart) {
                case 0:
                    if (intro.keepRunning()) {
                        intro.update(difTime);
                        intro.draw();
                    } else {
                        demoPart++;
                    }
                    break;
                case 1:
                    if (part1.keepRunning()) {
                        part1.update(difTime);
                        part1.draw();
                    } else {
                        demoPart++;
                    }
                    break;
                default:
                    fs.doQuit();
                    break;
            }

        }

        // End the program.
        music.close();
    }
}
